package com.hiwan.unions.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.hiwan.unions.activity.main.homeInform.InformActivity;
import com.hiwan.unions.activity.membership.MembershipRealActivity;
import com.hiwan.unions.activity.membership.MembershipTypeActivity;
import com.hiwan.unions.activity.membership.MembershipshenheActivity;
import com.hiwan.unions.activity.system.FeedbackActivity;
import com.hiwan.unions.activity.system.MembershipActivity;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONObject;

/**
 * Created by llg on 2017/7/17.
 */
public class StartIntent {
    Context c;
    protected static SharedPreferences userInfo;
    private String title;
    private static StartIntent startIntent;

    public static StartIntent get(Context ctx) {
        if (startIntent == null) {
            startIntent = new StartIntent(ctx);
            userInfo = ctx.getSharedPreferences(ConstantsValues.INIT_USER_INFO, Context.MODE_PRIVATE);

        }
        return startIntent;
    }

    public StartIntent(Context c) {
        this.c = c;
    }

    public void contentStart(String title, int i) {
        if (title.equals("往期动态")) {
            InformActivity.startPreview(c, i);
        } else {
            contentStart(title);
        }
    }

    public boolean contentStart(String title) {
        String url = "";
        if (title.equals("入会申请") && CheckUtils.userInfo(c) || title.equals("会籍信息")) {
            this.title = title;
            Request<JSONObject> request = NoHttp.createJsonObjectRequest(ConstsNet.validaMebStaByMobile, RequestMethod.POST);
            request.add("mobile", userInfo.getString(ConstantsValues.phone, ""));
            CallServer.getRequestInstance().addToken((Activity) c, 0, request, objectListener, true, userInfo);
        } else if (title.equals("留言板") && CheckUtils.userInfo(c)) {
            FeedbackActivity.startPreview(c, title);
        } else if (title.equals("电子书屋") && CheckUtils.userInfo(c)) {
            long t = System.currentTimeMillis();
            Logger.i("uuid  " + AESKit.decodebase64AndDecrypt(userInfo.getString(ConstantsValues.uuid, ""), ConstantsValues.ASEKIPASSWORD));
            String sign = MD5Utils.encrypt(AESKit.decodebase64AndDecrypt(userInfo.getString(ConstantsValues.uuid, ""), ConstantsValues.ASEKIPASSWORD) + t + "D3cBcAM4BzcAdlExXHAEMFA0UWlYbQ09U3FSLFYuVz5XJgM3ViZTPVYgBG8AOQQwBGQDLVB/VnEJZ1BjUWZSMQ8uAWgDcwc7AD5ROVxjBDBQJ1FxWHkNfFM6Um1WaVdzV24DYVYxUzdWNwRvADYEMgRhAzpQYlYu");
            url = "http://wk.bookan.com.cn/index.php?op=Authentication.redirect&authType=1&id=21465&userName=" + AESKit.decodebase64AndDecrypt(userInfo.getString(ConstantsValues.uuid, ""), ConstantsValues.ASEKIPASSWORD) + "&time=" + t + "&sign=" + sign + "";
            Utils.callHelpDeilsActivity(url, c, "");
            return true;
        } else {
            if (title.equals("互助保障")) {
                url = "pages/cooperation.html";
            } else if (title.equals("困难帮扶")) {
                url = "pages/difficulty.html";
            } else if (title.equals("就业服务")) {
                url = "pages/employmentService.html";
            } else if (title.equals("创业服务")) {
                url = "pages/EntrepreneurshipService.html";
            } else if (title.equals("心灵驿站")) {
                url = "pages/heart.html";
            } else if (title.equals("法律援助")) {
                url = "pages/layAid.html";
            } else if (title.equals("法律咨询")) {
                url = "pages/legalAdvice.html";
            } else if (title.equals("妈咪宝贝") || title.equals("妈咪宝贝屋")) {
                url = "pages/MamyPoko.html";
            } else if (title.equals("疗养院")) {
                url = "pages/sanatorium.html";
            } else if (title.equals("服务阵地")) {
                url = "pages/servicePosition.html";
            } else if (title.equals("技能培训")) {
                url = "pages/skill.html";
            } else if (title.equals("视频课堂")) {
                url = "pages/videoClassroom.html";
            } else if (title.equals("婚恋交友")) {
                url = "pages/wedlock.html";
            } else if (title.equals("劳动法律知识库")) {
                url = "pages/LegalKnowledgeBase.html";
            } else if (title.equals("劳动保护")) {
                url = "pages/laborProtection.html";
            } else if (title.equals("政策查询")) {
                url = "pages/policyInquiryList.html";
            } else if (title.equals("惠普资讯")) {
                url = "news/Investment.html";
            } else if (title.equals("我的优惠券")) {
                url = "activity/coupon.html";
            } else if (title.equals("我的活动")) {
                url = "pages/whitneyActivitiesMy.html";
            } else if (title.equals("市州动态")) {
                url = "pages/allgh.html";
            } else if (title.equals("劳模风采")) {
                url = "pages/laborStyle.html";
            } else if (title.equals("投诉建议") && CheckUtils.userInfo(c)) {
                url = "pages/complaintApply.html";
            }
            if (url.equals("")) {
                return false;
            } else {
                Utils.callHelpDeilsActivity(ConstsNet.LILINTONG_URLWEB + url, c, "");
                return true;
            }
        }
        return true;
    }

    private HttpListener<JSONObject> objectListener = new HttpListener<JSONObject>() {
        @Override
        public void onSucceed(int what, Response<JSONObject> response) {
            JSONObject jsonObject = response.get();
            jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
            String type = JsonUtil.getString(jsonObject, "menmberType");
            if (JsonUtil.getString(jsonObject, "memberStatus").equals("0")) {
                if (title.equals("会籍信息")) {
                    MembershipActivity.startPreview(c);
                } else {
                    MembershipTypeActivity.startPreview(c, title, true, false);
                }
            } else if (JsonUtil.getString(jsonObject, "memberStatus").equals("1")) {
                MembershipshenheActivity.startPreview(c, title);
            } else if (JsonUtil.getString(jsonObject, "memberStatus").equals("2")) {
                if (title.equals("会籍信息")) {
                    MembershipActivity.startPreview(c);
                } else {
                    MembershipTypeActivity.startPreview(c, title, true, false);
                }
            } else if (JsonUtil.getString(jsonObject, "memberStatus").equals("3")) {
                if (type.equals("0")) {
                    MembershipTypeActivity.startPreview(c, title, false, false);
                } else {
                    MembershipTypeActivity.startPreview(c, title, false, true);
                }
            } else if (JsonUtil.getString(jsonObject, "memberStatus").equals("9")) {
                MembershipRealActivity.startPreview(c, title);
            }
        }

        @Override
        public void onFailed(int what, int errCode, String networkMillis, boolean type) {

        }
    };

}
